//
//  CountDownModeLevelBaseLayer.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/26.
//
//

#include "CountDownModeLevelBaseLayer.h"


#define MATRIX_WIDTH (9)
#define MATRIX_HEIGHT (9)


#define MONSTER_VIEW_XPAD  0
#define MONSTER_VIEW_TOPPAD  0
#define CRUSH_VIEW_X   0
#define CRUSH_VIEW_Y   80

#define PAUSE_BUTTON_X    250
#define PAUSE_BUTTON_Y    500

typedef enum {
    ZBg = 0,
 //   ZMonsterView,
    ZCountDownModeCrushView,
    ZMenu,
    ZStartView,
    ZEndView,

} Zindex;

typedef enum {
    TagMenu = 1,
    TagPause,
    TagRun,
    
    
} MenuTag;

CountDownModeLevelBaseLayer::CountDownModeLevelBaseLayer()
:m_isPause(false)
,monsterType(0)
,bad_matrix(NULL)
,bad_matrixUp(NULL)
,m_level(0)
,m_bombNum(0)
{
}

CountDownModeLevelBaseLayer::~CountDownModeLevelBaseLayer()
{
    if(bad_matrix){
    	free(bad_matrix);
    }

    if(bad_matrixUp){
    	free(bad_matrixUp);
    }
    if(jelly_Matrix){
    	free(jelly_Matrix);
    }

}

bool CountDownModeLevelBaseLayer::baseInit()
{
    if (!Layer::init()) {
        return false;
    }
    
    bgInit();
    scoreBarViewInit();
    countDownTextInit();
    optionMenuViewInit();
    startPromptViewInit();
    return true;
}

void CountDownModeLevelBaseLayer::bgInit()
{
    // background
    Size winSize = Director::getInstance()->getWinSize();
    auto background = Sprite::create("background.png");
    background->setAnchorPoint(Point(0, 1));
    background->setPosition(Point(0, winSize.height));
    this->addChild(background,-1);
}

void CountDownModeLevelBaseLayer::startPromptViewInit()
{
    startPromptView = NULL;
    startPromptView = StartPromptView::create();
    startPromptView->setAnchorPoint(Vec2::ZERO);
    startPromptView->setLevel(m_level);
    startPromptView->setCallback(CC_CALLBACK_1(CountDownModeLevelBaseLayer::startPromptViewCallback, this));
    addChild(startPromptView,ZStartView);
    startPromptView->show();
}

void CountDownModeLevelBaseLayer::endPromptViewShow(bool success)
{
    endPromptView = NULL;
    endPromptView = EndPromptView::create();
    endPromptView->setAnchorPoint(Vec2::ZERO);
    addChild(endPromptView,ZEndView);
    endPromptView->show(success);
}


void CountDownModeLevelBaseLayer::countDownTextInit(){
	countDownText = NULL;
	Size size = Director::getInstance()->getWinSize();

	        Color4B c;
			c.a = 255;
			c.r = 140;
			c.g = 91;
			c.b = 0;
			countDownText = Label::create("", "Arial", 24);
			countDownText->setTextColor(c);
			__String *text = __String::createWithFormat(LocalizedStringGetter::getStringByKey("MoveStepLeft"),moveStepLeft);
			countDownText->setString(text->getCString());

			countDownText->setPosition(Point(size.width/2, size.height - 100));
			this->addChild(countDownText);

}

void CountDownModeLevelBaseLayer::setCountDownTextInit(int count){
	__String *text = __String::createWithFormat(LocalizedStringGetter::getStringByKey("MoveStepLeft"),count);
				countDownText->setString(text->getCString());
}

void CountDownModeLevelBaseLayer::startPromptViewCallback(Ref *sender)
{
	CountDownModeCrushViewInit();
	scheduleUpdate();
}

void CountDownModeLevelBaseLayer::CountDownModeCrushViewInit()
{
    countDownModeCrushView = CountDownModeCrushView::create();
    countDownModeCrushView->setAnchorPoint(Vec2::ZERO);
    countDownModeCrushView->setPosition(Point(CRUSH_VIEW_X,CRUSH_VIEW_Y));

    countDownModeCrushView->setTypeNumLimit(typeNumLimit);

    if(bad_matrixBlank){
       	countDownModeCrushView->initMatrixBlank(bad_matrixBlank);
        }
    countDownModeCrushView->initMatrix(jelly_Matrix);
    if(bad_matrix){
    	countDownModeCrushView->initMatrixBad(bad_matrix);
    }
    if(bad_matrixUp){
    	countDownModeCrushView->initMatrixBadUp(bad_matrixUp);
    }

    countDownModeCrushView->setBombNum(m_bombNum);
    countDownModeCrushView->setUpdateCallback(CC_CALLBACK_1(CountDownModeLevelBaseLayer::updateCallback, this));
    countDownModeCrushView->setCallback(CC_CALLBACK_1(CountDownModeLevelBaseLayer::crushCallback, this));
    countDownModeCrushView->setBadCallback(CC_CALLBACK_1(CountDownModeLevelBaseLayer::crushBadStateCallback, this));
    addChild(countDownModeCrushView,ZCountDownModeCrushView);
    
}

void CountDownModeLevelBaseLayer::crushCallback(Ref *sender)
{

	moveStepLeft--;
	setCountDownTextInit(moveStepLeft);
	scoreBarView->setPercentage(moveStepLeft*100/moveStepLimit);
	if(countDownModeCrushView != NULL && countDownModeCrushView->getBadStateNum() == 0){
		scoreBarView->recordTime();
//		Director::getInstance()->replaceScene(ShowWinScreen::createScene());
		countDownModeCrushView->unscheduleUpdate();
		endPromptViewShow(true);
		return;
	}
	if(moveStepLeft ==0){
	//	Director::getInstance()->replaceScene(ShowFailScreen::createScene());
		countDownModeCrushView->unscheduleUpdate();
		endPromptViewShow(false);
		return;
	}
}

void CountDownModeLevelBaseLayer::updateCallback(Ref *sender)
{

	//moveStepLeft--;
	//setCountDownTextInit(moveStepLeft);
//	scoreBarView->setPercentage(moveStepLeft*100/moveStepLimit);
	if(countDownModeCrushView != NULL && countDownModeCrushView->getBadStateNum() == 0){
		scoreBarView->recordTime();
		countDownModeCrushView->unscheduleUpdate();
	//	Director::getInstance()->replaceScene(ShowWinScreen::createScene());
		endPromptViewShow(true);
	}
	if(moveStepLeft ==0){
	//	Director::getInstance()->replaceScene(ShowFailScreen::createScene());
		countDownModeCrushView->unscheduleUpdate();
		endPromptViewShow(false);
	}
}

void CountDownModeLevelBaseLayer::crushBadStateCallback(Ref *sender)
{

}

void CountDownModeLevelBaseLayer::monsterViewBossAttackCallbackFun(Ref *sender)
{
	countDownModeCrushView->bossCreateJellys();
}

void CountDownModeLevelBaseLayer::optionMenuViewInit()
{
    optionMenuView = OptionMenuView::create();
    optionMenuView->setPosition(Vec2::ZERO);
    addChild(optionMenuView,ZMenu);
}

bool CountDownModeLevelBaseLayer::isBadStateMatrixNull(){
	if(!bad_matrix)return true;

	  for (int col = 0; col < MATRIX_WIDTH; col++) {
	        for (int row = 0; row < MATRIX_HEIGHT; row++) {
	            if(bad_matrix[row * MATRIX_WIDTH + col]){
	            	return false;
	            }
	        }
	  }
	  return true;
}


void CountDownModeLevelBaseLayer::scoreBarViewInit()
{
    Size size = Director::getInstance()->getWinSize();
    scoreBarView = ScoreBarView::create(HeroUsed::getInstance()->getHeroHealth());
    scoreBarView->setPosition(Point((size.width - TIME_BAR_VIEW_WIDTH)/2,
                                   TIME_BAR_YPAD));
    scoreBarView->setCallback(CC_CALLBACK_1(CountDownModeLevelBaseLayer::scoreBarViewCallback, this));
    addChild(scoreBarView,ZMenu);
    scoreBarView->setPause(true);
}

void CountDownModeLevelBaseLayer::scoreBarViewCallback(Ref *sender){
//	 Director::getInstance()->replaceScene(ShowFailScreen::createScene());
}
